package com.taiyi.renaissancemall.ui.mine.scores;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.MyScoresBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import butterknife.OnClick;

//积分页面
public class MyScoresActivity extends BaseActivity<MyScoresView, MyScoresPresent> implements MyScoresView, SpringView.OnFreshListener {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightTitleTv)
    TextView rightTitleTv;
    @BindView(R.id.topB)
    TextView topB;
    @BindView(R.id.totalPoints)
    TextView totalPoints;
    @BindView(R.id.totalPointsTv)
    TextView totalPointsTv;
    @BindView(R.id.getPointsTv)
    TextView getPointsTv;
    @BindView(R.id.integralFlow)
    TextView integralFlow;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.top)
    ConstraintLayout top;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;
    private int page = 1;
    private MyScoresAdapter myScoresAdapter;
    private List<MyScoresBean> data = new ArrayList<>();
    private String scorenum;
    private List<MyScoresBean> comlist;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_scores;
    }

    @Override
    public MyScoresPresent initPresenter() {
        return new MyScoresPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        titleTv.setText(getString(R.string.myScores));
//        springView.setHeader(new DefaultHeader(this));
//        springView.setFooter(new DefaultFooter(this));
//        springView.setGive(SpringView.Give.BOTH);
//        springView.setType(SpringView.Type.FOLLOW);
//        springView.setListener(this);
        setAdapter(data);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).statusBarDarkFont(false, 1.0f).init();
    }

    @Override
    protected void initData() {
        scorenum = getIntent().getStringExtra("scorenum");
        if ("".equals(scorenum) || scorenum == null) {
            totalPointsTv.setText("0.0");
        } else {
            totalPointsTv.setText(scorenum);
        }
        showProgress();
        //请求积分数据
        presenter.getMyScores("6", page + "");//1预结算礼券2可提现礼券6积分
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void MyScoresSuccess(List<MyScoresBean> myScoresBean) {
        hideProgress();
        springView.onFinishFreshAndLoad();
        comlist = myScoresBean;
        if (page == 1) {
            data.clear();
            if (comlist == null || comlist.size() == 0) {
                noDataIv.setVisibility(View.VISIBLE);
            } else {
                noDataIv.setVisibility(View.GONE);
            }
        }
        data.addAll(comlist);
        if (myScoresAdapter != null) {
            myScoresAdapter.notifyDataSetChanged();
        }

    }

    private void setAdapter(List<MyScoresBean> dataBeanList) {
        myScoresAdapter = new MyScoresAdapter(R.layout.mine_my_wallet_item_layout, dataBeanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(MyScoresActivity.this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(myScoresAdapter);
    }

    @Override
    public void MyScoresFailure(String msg) {
        hideProgress();
        ToastUtils.showLongToast(msg);
    }

    @Override
    public void onRefresh() {
        page = 1;
        presenter.getMyScores("6", page + "");
    }

    @Override
    public void onLoadmore() {
        page++;
        presenter.getMyScores("6", page + "");
    }

    @OnClick({R.id.backIv})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.backIv) {
            finish();
        }
    }

}
